from typing import List

class Solution:
    def numOfMinutes(self, n: int, headID: int, manager: List[int], informTime: List[int]) -> int:
        minus = [-1] * n
        def dfs(id):
            if id == headID:
                return 0
            if minus[id] != -1:
                return minus[id]
            minus[id] = informTime[manager[id]] + dfs(manager[id])
            return minus[id]
        
        ans = 0
        for i in range(n):
            if i == headID:
                continue
            ans = max(ans, dfs(i))
        return ans